package com.boho.system.service;

/**
 * @File name:  RoleService.java 
 * @Create on:  2010-05-10 19:30:171
 * @Author   :  陈伟
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 */


import java.sql.Connection;
import java.util.List;

import cn.boho.framework.context.ActionContext;
import cn.boho.framework.exception.UserOperationException;
import cn.boho.framework.log.Logger;
import cn.boho.framework.po.ComboPager;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.POUtils;
import cn.boho.framework.po.Pager;
import cn.boho.framework.service.MessageService;

import com.boho.common.Constant;
import com.boho.common.SystemUtils;
import com.boho.system.dao.TSysRoleDao;
import com.boho.system.po.TRolePO;

/**
 * 角色管理业务类
 * @author 陈伟
 *
 */
public class RoleService {

	private static Logger logger = new Logger(org.apache.log4j.Logger.getLogger(RoleService.class));
	
	/**
	 * 添加角色信息
	 * @param atx
	 * @param rolename
	 * @param roledesc
	 * @return
	 * @throws Exception
	 */
	public static String addRole(ActionContext atx, String rolename, String roledesc) throws Exception{
		SystemUtils.getSessionUser(atx);
		Connection con = atx.getConection();
		//判断角色是否存在
		checkRoleIsExist(atx,rolename);
		//添加角色
		StringBuffer roleID = new StringBuffer();
		TRolePO tSysRolePO = new TRolePO();
		tSysRolePO.setRoleName(rolename);
		tSysRolePO.setRoleDesc(roledesc);
		POUtils.insert(con, tSysRolePO, roleID);
		//日志
		StringBuffer info = new StringBuffer();
		info.append("\n【添加角色】")
			.append("\nIP："+atx.getRemoteAddr())
			.append("\n操作用户ID："+atx.getSessionValue(Constant.SESSION_USER_NO).toString())
			.append("\n"+tSysRolePO.toXMLStringBuffer().toString());
		logger.info(info.toString());
		return roleID.toString();
	}
	
	/**
	 * 判断角色名称是否存在
	 * @param con
	 * @param rolename
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static DynaBeanMap checkRoleIsExist(ActionContext atx, String rolename) throws Exception{
		DynaBeanMap dbm = TSysRoleDao.getRoleByRoleName(atx.getConection(), rolename);
		if(dbm != null){
			UserOperationException uoe = new UserOperationException();
			atx.setErrorContext("FSTI_SYS_ROLE_1001", MessageService.getMessage("FSTI_SYS_ROLE_1001"),uoe);
		}
		return dbm;
	}
	
	/**
	 * 修改角色信息时检查除本身外其它角色名称是否与当前值重名
	 * @param atx
	 * @param rolename
	 * @param roleid
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static DynaBeanMap checkRoleIsExistByUpdate(ActionContext atx, String rolename, Integer roleid) throws Exception{
		DynaBeanMap dbm = TSysRoleDao.getRoleByRoleNameByUpdate(atx.getConection(), rolename, roleid);
		UserOperationException uoe = new UserOperationException();
		if(dbm != null){
			atx.setErrorContext("FSTI_SYS_ROLE_1001", MessageService.getMessage("FSTI_SYS_ROLE_1001"),uoe);
		}
		return dbm;
	}
	
	/**
	 * 根据角色名称查询角色列表
	 * @param atx
	 * @param pager
	 * @param rolename
	 * @return
	 * @throws Exception
	 */
	public static ComboPager queryRoleByRoleName(ActionContext atx, Pager pager, String rolename) throws Exception{
		return TSysRoleDao.queryTSysRoleByPager(atx.getConection(),pager,rolename);
	}
	
	/**
	 * 修改角色信息
	 * @param atx
	 * @param roleid
	 * @param rolename
	 * @param roledesc
	 * @throws Exception
	 */
	public static void updateRoleByPK(ActionContext atx, Integer roleid, String rolename, String roledesc) throws Exception{
		SystemUtils.getSessionUser(atx);
		Connection con = atx.getConection();
		//检查是否重名
		checkRoleIsExistByUpdate(atx, rolename, roleid);
		//修改角色信息
		TRolePO conditionTSysRolePO = new TRolePO();
		TRolePO valueTSysRolePO = new TRolePO();
		conditionTSysRolePO.setRoleId(roleid);
		valueTSysRolePO.setRoleName(rolename);
		valueTSysRolePO.setRoleDesc(roledesc);
		POUtils.update(con, conditionTSysRolePO, valueTSysRolePO,false);
		//日志
		StringBuffer info = new StringBuffer();
		info.append("\n【修改角色】")
			.append("\nIP："+atx.getRemoteAddr())
			.append("\n操作用户ID："+atx.getSessionValue(Constant.SESSION_USER_NO).toString())
			.append("\n修改对象"+conditionTSysRolePO.getRoleId())
			.append("\n修改内容："+valueTSysRolePO.toXMLStringBuffer().toString());
		logger.info(info.toString());
	}
	
	/**
	 * 删除角色信息
	 * @param atx
	 * @param roleid
	 * @throws Exception
	 */
	public static void deleteRoleByPK(ActionContext atx, Integer roleid) throws Exception {
		SystemUtils.getSessionUser(atx);
		Connection con = atx.getConection();
		TRolePO tRolePO = new TRolePO();
		tRolePO.setRoleId(roleid);
		POUtils.delete(con, tRolePO);
		//日志
		StringBuffer info = new StringBuffer();
		info.append("\n【删除角色】")
			.append("\nIP："+atx.getRemoteAddr())
			.append("\n操作用户ID："+atx.getSessionValue(Constant.SESSION_USER_NO).toString())
			.append("\n删除对象"+tRolePO.getRoleId());
		logger.info(info.toString());
	}
	
	/**
	 * 批量删除角色信息
	 * @param atx
	 * @param roleid
	 * @throws Exception
	 */
	public static void batchDeleteRoleByPK(ActionContext atx, Integer[] roleids) throws Exception{
		SystemUtils.getSessionUser(atx);
		if(roleids!=null){
			Connection con = atx.getConection();
			StringBuffer logPKRole = new StringBuffer();
			for (Integer roleid : roleids) {
				if(roleid == Constant.ADMIN_ROLE){
					UserOperationException uoe = new UserOperationException();
					atx.setErrorContext("FSTI_SYS_ROLE_1002", MessageService.getMessage("FSTI_SYS_ROLE_1002"), uoe);
					throw uoe;
				}
				logPKRole.append(roleid).append(";");
				TRolePO tRolePO = new TRolePO();
				tRolePO.setRoleId(roleid);
				POUtils.delete(con, tRolePO);
			}
			
			//日志
			StringBuffer info = new StringBuffer();
			info.append("\n【批量删除角色】")
				.append("\nIP："+atx.getRemoteAddr())
				.append("\n操作用户ID："+atx.getSessionValue(Constant.SESSION_USER_NO).toString())
				.append("\n删除对象："+logPKRole.toString());
			logger.info(info.toString());
		}
	}
	
	/**
	 * 根据角色ID查询信息
	 * @param atx
	 * @param roleid
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static DynaBeanMap getRoleByPK(ActionContext atx, Integer roleid) throws Exception{
		return TSysRoleDao.getTSysRoleByPk(atx.getConection(), roleid);
	}
	
	/**
	 * 查询所有角色列表
	 * @param atx
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static List queryRoleList(ActionContext atx) throws Exception{
		return TSysRoleDao.queryTSysRole(atx.getConection());
	}
}
